<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>晴天医诊</title>
    <meta name="description" content="晴天医诊">
    <meta name="keywords" content="晴天医诊">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <!-- 引入组件库 -->
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script src="../js/vue.js"></script>
    <script src="../plugins/elementui/index.js"></script>
    <script src="../js/axios-0.18.0.js"></script>
    <style>
        .datatable {
            position: relative;
            box-sizing: border-box;
            -webkit-box-flex: 1;
            width: 100%;
            max-width: 100%;
            font-size: 14px;
            color: rgb(96, 98, 102);
            overflow: hidden;
            flex: 1 1 0%;
        }

        .datatable td, .datatable th {
            padding: 12px 0;
            min-width: 0;
            -webkit-box-sizing: border-box;
            box-sizing: border-box;
            text-overflow: ellipsis;
            vertical-align: middle;
            position: relative;
            text-align: left;
        }

        .el-dropdown-link {
            cursor: pointer;
            color: #409EFF;
        }

        .el-icon-arrow-down {
            font-size: 12px;
        }

    </style>
</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>系统设置<small>菜单管理</small></h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>系统设置</el-breadcrumb-item>
            <el-breadcrumb-item>菜单管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <el-input placeholder="菜单名/linkUrl" v-model="pagination.queryString" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter"></el-input>
                <el-button @click="findPage()" class="dalfBut">查询</el-button>
                <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
            </div>
            <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                <el-table-column type="index" align="center" label="序号"></el-table-column>
                <el-table-column prop="name" label="菜单名" align="center"></el-table-column>
                <el-table-column prop="linkurl" label="linkUrl" align="center"></el-table-column>
                <el-table-column label="父菜单" align="center">
                    <template slot-scope="scope">
                        <span>{{scope.row.level == 1 ? '父菜单' : '子菜单'}}</span>
                    </template>
                </el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                        <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>

            <el-dialog title="新增" :visible.sync="dialogRadio">
                <el-radio-group v-model="radio" @change="agreeChange">
                    <el-radio class="radio" label="1" border>添加父节点</el-radio>
                    <el-radio class="radio" label="2" border>添加子节点</el-radio>
                </el-radio-group>
            </el-dialog>


            <!-- 新增标签弹层（父菜单）-->
            <div class="add-form">
                <el-dialog title="新增父菜单" :visible.sync="dialogFormVisible">
                    <template>
                        <el-tabs v-model="activeName" type="card">
                            <el-tab-pane label="基本信息" name="first">
                                <el-form label-position="right" label-width="100px">
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="菜单名">
                                                <el-input v-model="formData.name"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="ICON">
                                                <el-input v-model="formData.icon"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="PATH">
                                                <el-input v-model="formData.path"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="描述">
                                                <el-input v-model="formData.description" type="textarea"></el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                </el-form>
                            </el-tab-pane>
                        </el-tabs>
                    </template>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
            <!-- 新增标签弹层（子菜单） -->
            <div class="add-form">
                <el-dialog title="新增子菜单" :visible.sync="dialogFormVisibleChildren">
                    <template>
                        <el-tabs v-model="activeName" type="card">
                            <el-tab-pane label="基本信息" name="first">
                                <el-form label-position="right" label-width="100px">
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="菜单名">
                                                <el-input v-model="formData.name"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="LINKURL">
                                                <el-input v-model="formData.linkurl"/>
                                                <el-input v-model="formData.parentmenuid" type="hidden"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="菜单名">

                                                <el-dropdown @command="handleCommand">
                                                            <span class="el-dropdown-link">
                                                                父菜单<i class="el-icon-arrow-down el-icon--right"></i>
                                                            </span>
                                                    <el-dropdown-menu slot="dropdown">
                                                        <el-dropdown-item :command=p v-for="p in ParentList">
                                                            {{p.name}}
                                                        </el-dropdown-item>
                                                    </el-dropdown-menu>
                                                </el-dropdown>

                                                {{ParentValue}}

                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="描述">
                                                <el-input v-model="formData.description" type="textarea"></el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                </el-form>
                            </el-tab-pane>
                        </el-tabs>
                    </template>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisibleChildren = false">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

            <!-- 编辑标签弹层（父菜单） -->
            <div class="add-form">
                <el-dialog title="编辑菜单" :visible.sync="dialogFormVisible4Edit">
                    <template>
                        <el-tabs v-model="activeName" type="card">
                            <el-tab-pane label="基本信息" name="first">
                                <el-form label-position="right" label-width="100px">
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="菜单名">
                                                <el-input v-model="formData.name"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="ICON">
                                                <el-input v-model="formData.icon"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="PATH">
                                                <el-input v-model="formData.path"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="描述">
                                                <el-input v-model="formData.description" type="textarea"></el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                </el-form>
                            </el-tab-pane>
                        </el-tabs>
                    </template>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                        <el-button type="primary" @click="handleEdit()">确定</el-button>
                    </div>
                </el-dialog>
            </div>


            <!-- 编辑标签弹层 （子菜单）-->
            <div class="add-form">
                <el-dialog title="编辑菜单" :visible.sync="dialogFormVisible4EditChildren">
                    <template>
                        <el-tabs v-model="activeName" type="card">
                            <el-tab-pane label="基本信息" name="first">
                                <el-form label-position="right" label-width="100px">
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="菜单名">
                                                <el-input v-model="formData.name"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="LINKURL">
                                                <el-input v-model="formData.linkurl"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="菜单名">
                                                <el-dropdown @command="handleCommand">
                                                            <span class="el-dropdown-link">
                                                                父菜单<i class="el-icon-arrow-down el-icon--right"></i>
                                                            </span>
                                                    <el-dropdown-menu slot="dropdown">
                                                        <el-dropdown-item :command=p v-for="p in ParentList">
                                                            {{p.name}}
                                                        </el-dropdown-item>
                                                    </el-dropdown-menu>
                                                </el-dropdown>

                                                {{ParentValue}}

                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="描述">
                                                <el-input v-model="formData.description" type="textarea"></el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                </el-form>
                            </el-tab-pane>
                        </el-tabs>
                    </template>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                        <el-button type="primary" @click="handleEdit()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
        </div>
    </div>
</div>
</body>

<script>
    var vue = new Vue({
        el: '#app',
        data: {
            activeName: 'first',//添加/编辑窗口Tab标签名称
            dataList: [],//列表数据
            formData: {},//表单数据
            dialogRadio: false,
            radio: null,
            dialogFormVisible: false,//控制添加窗口显示/隐藏（父菜单）
            dialogFormVisibleChildren: false,//控制添加窗口显示/隐藏（子菜单）
            dialogFormVisible4Edit: false,//控制编辑窗口显示/隐藏（父菜单）
            dialogFormVisible4EditChildren: false,//控制编辑窗口显示/隐藏（子菜单）
            ParentValue: null,//显示父节点名称
            ParentList: null,//父节点集合
            IsId: null,//判断选择是添加父菜单还是子菜单(1：父 2：子)
            pagination: {//分页相关属性
                currentPage: 1,
                pageSize: 10,
                total: 100,
                queryString: null,
            },

        },
        created() {
            this.findPage();
            this.findByParent()

        },
        methods: {
            //查询所有父级菜单
            findByParent() {
                axios.get("/menu/findByParent.do").then(res => {
                    console.log(res)
                    this.ParentList = res.data.data;
                    this.ParentValue = res.data.data.id;
                })
            }
            ,
            agreeChange: function (val) {
                if (val == 1) {//添加父节点
                    this.resetForm();
                    this.dialogFormVisible = true;//显示父节点
                    this.dialogRadio = false;//关闭单选框
                    this.IsId = 1;
                }
                if (val == 2) {//添加子节点
                    this.resetForm();
                    this.dialogFormVisibleChildren = true;//显示子节点
                    this.dialogRadio = false;//关闭单选框
                    this.IsId = 2;
                    axios.post("/menu/findByParent.do").then(res => {
                        if (res.data.flag) {
                            this.ParentList = res.data.data
                        } else {
                            this.$message.error(res.data.message);
                        }
                    })
                }
            },
            handleCommand(command) {
                this.formData.parentmenuid = command.id
                this.ParentValue = command.name//获取当前选中的父名称
            },
            //编辑
            handleEdit() {
                axios.post("/menu/update.do", this.formData).then(res => {
                    if (res.data.flag) {//成功了
                        this.$message({
                            message: res.data.message,//弹出编辑成功消息
                            type: 'success'
                        });
                        if (this.IsId == 1) {
                            this.dialogFormVisible4Edit = false;//提交后隐藏添加标签弹层
                        } else {
                            this.dialogFormVisible4EditChildren = false;//提交后隐藏添加标签弹层
                        }
                        this.findPage();//刷新数据
                    } else {
                        this.$message.error(res.data.message);//弹出添加失败消息
                    }
                });
            },
            //添加
            handleAdd() {
                axios.post("/menu/insert.do?IsId=" + this.IsId, this.formData).then(res => {
                    if (res.data.flag) {//成功了
                        this.$message({
                            message: res.data.message,//弹出添加成功消息
                            type: 'success'
                        });
                        if (this.IsId == 1) {
                            this.dialogFormVisible = false;//提交后隐藏添加标签弹层
                        } else {
                            this.dialogFormVisibleChildren = false;//提交后隐藏添加标签弹层
                        }
                        this.findPage();//刷新数据
                    } else {
                        console.log(res)
                        this.$message.error(res.data.message);//弹出添加失败消息
                    }
                })

            },
            //查询所有
            findPage() {
                axios.post("/menu/findPage.do", this.pagination).then(res => {
                    if (res.data.flag) {
                        this.dataList = res.data.data.rows;
                        //    總記錄數
                        this.pagination.total = res.data.data.total;
                    } else {
                        this.$message.error(res.data.message);
                    }
                })

            },
            // 重置表单
            resetForm() {
                //清空表单数据
                this.formData = {}
                this.activeName = 'first'; //选中基本信息标签项
                this.radio = null;
                this.ParentValue = null;
                this.ParentList = null;
                this.IsId = null;
            },
            // 弹出添加窗口
            handleCreate() {
                this.resetForm();
                this.dialogRadio = true


            },
            //切换页码
            handleCurrentChange(currentPage) {
                this.pagination.currentPage = currentPage;
                this.findPage()
            },
            // 弹出编辑窗口
            handleUpdate(row) {
                this.resetForm();

                axios.post("/menu/queryById.do?id=" + row.id).then((res) => {
                    if (res.data.flag) {
                        if (res.data.data.menu.level == 1) {//父菜单
                            this.IsId = 1;
                            this.dialogFormVisible4Edit = true;//显示父节点
                            //绑定数据
                            this.formData = res.data.data.menu;
                        }
                        if (res.data.data.menu.level == 2) {//子菜单
                            this.ParentList = res.data.data;
                            console.log(this.ParentList.menu.parentmenuid)
                            this.IsId = 2;
                            this.dialogFormVisible4EditChildren = true;//显示父节点
                            this.ParentValue = this.ParentList.menu.parentmenuid;
                            axios.post('/menu/findByParentId.do?id=' + this.ParentValue).then(res => {
                                console.log("========>")
                                console.log(res.data)
                                this.ParentValue = res.data.data.name;
                            })
                            axios.get("/menu/findByParent.do").then(res => {
                                this.ParentList = res.data.data;
                            })
                            //绑定数据
                            this.formData = res.data.data.menu;
                            // this.formData.parentmenuid = res.data.data.menuParent.id;
                            // this.ParentValue = res.data.data.menuParent.name//获取当前选中的父名称

                        }
                    } else {
                        this.$message.error("获取数据失败，请刷新当前页面");
                    }
                });


            },
            // 删除
            handleDelete(row) {
                this.$confirm('是否删除该检查组, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {//确定
                    axios.post("/menu/deleteById.do?id="+row.id).then(res=>{
                        if(res.data.flag){
                            this.$message({
                                message: res.data.message,//弹出编辑成功消息
                                type: 'success'
                            });
                            this.findPage();//刷新数据
                        } else {
                            this.$message.error(res.data.message);//弹出删除失败消息
                        }
                    })
                }).catch(() => {//取消
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            }
        }
    })
</script>
</html>
